package com.easy.dfcshop.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.core.web.BaseController;
import com.easy.dfcshop.response.ImageRes;
import com.easy.dfcshop.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 用户相关控制器
 */

@Controller
@RequestMapping(value = "${adminPath}/image")
public class ImageController extends BaseController {


    @Autowired
    private ImageService imageService;

    /**
     * 上传产品图片
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "UploadImg", method = RequestMethod.POST)
    public Result UploadImg(ImageRes image, HttpServletRequest request) {

        try {
            return imageService.UploadImg(image, request);

        } catch (IOException e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.UPLOAD_FAILD);
            return rt;

        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }


}
